package so.shanku.essential.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.fragment.*;
import so.shanku.essential.utils.Constant;

import java.util.HashMap;
/**
 * Created by xiaozhen on 2016/3/8.
 * 我的收藏
 */
public class UserCollectionActivity extends BaseUIActivity {
	private int currentSelectItem = -1;
	//底部导航栏
	@ViewInject(id = R.id.mycollect_tab_top_layout)
	private RadioGroup mycollect_tab_top_layout;

	/**
	 * 保存点击过得fragment
	 */
	private HashMap<Integer, BaseFragment> fragments = new HashMap();


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acitivity_user_collection);
		mycollect_tab_top_layout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				setSelectItem(checkedId);
			}
		});
		setSelectItem(0);
	}


	//设置选中项和展示
	public void setSelectItem(int checkedId) {
		int selectItem = 0;
		if (checkedId == R.id.artical_rb) {
			selectItem = 0;
		} else if (checkedId == R.id.collection_selection_page_tv) {
			selectItem = 1;
		} else if (checkedId == R.id.collection_shop_page_tv) {
			selectItem = 2;
		} else if (checkedId == R.id.collection_product_page_tv) {
			selectItem = 3;
		} else if (checkedId == R.id.collection_daily_page_tv) {
			selectItem = 4;
		}

		if (currentSelectItem != selectItem) {

			currentSelectItem = selectItem;
			switch (selectItem) {
				case 0:
					if (fragments.get(selectItem) != null) {
						replaceView(fragments.get(selectItem));
					} else {
						ArticalFragment articalFragment = new ArticalFragment();
						fragments.put(selectItem, articalFragment);
						Bundle bundle=new Bundle();
						bundle.putString(Constant.ID,"-1");
						articalFragment.setArguments(bundle);
						replaceView(articalFragment);

					}

					break;
				case 1:
					if (fragments.get(selectItem) != null) {
						replaceView(fragments.get(selectItem));
					} else {
						SpecialSubjectFragment specialSubjectFragment = new SpecialSubjectFragment();
						fragments.put(selectItem, specialSubjectFragment);
						replaceView(specialSubjectFragment);
//                        FindFragment findFragment = new FindFragment();
//                        fragments.put(selectItem, findFragment);
//                        replaceView(findFragment);
					}
					break;
				case 2:
					if (fragments.get(selectItem) != null) {
						replaceView(fragments.get(selectItem));
					} else {
						BrandAllFragment brandAllFragment = new BrandAllFragment();
						fragments.put(selectItem, brandAllFragment);
						replaceView(brandAllFragment);
					}
					break;
				case 3:
					if (fragments.get(selectItem) != null) {
						replaceView(fragments.get(selectItem));
					} else {
						CommunityFragment shoppingBrandFragment = new CommunityFragment();
						fragments.put(selectItem, shoppingBrandFragment);
						replaceView(shoppingBrandFragment);
					}
					break;
				case 4:
					if (fragments.get(selectItem) != null) {
						replaceView(fragments.get(selectItem));
					} else {
						CommunityFragment shoppingBrandFragment = new CommunityFragment();
						fragments.put(selectItem, shoppingBrandFragment);
						replaceView(shoppingBrandFragment);
					}
					break;
				default:
					break;
			}

		}
	}


	private BaseFragment currentFragment;

	private void replaceView(BaseFragment fragment) {
		if (currentFragment != fragment) {
			FragmentTransaction transaction = this.getSupportFragmentManager()
					.beginTransaction();
//			FragmentTransaction transaction = mFragmentMan.beginTransaction().setCustomAnimations(
//					android.R.anim.fade_in, R.anim.slide_out);
			if (currentFragment != null) {
				if (!fragment.isAdded()) {    // 先判断是否被add过
					transaction.hide(currentFragment).add(R.id.fragment_layout, fragment).commit(); // 隐藏当前的fragment，add下一个到Activity中
				} else {
					transaction.hide(currentFragment).show(fragment).commit(); // 隐藏当前的fragment，显示下一个
				}
			} else {
				transaction.add(R.id.fragment_layout, fragment).commit(); // add下一个到Activity中
			}
			currentFragment = fragment;
		}
	}


}